//
// Created by yonghu0 on 25-10-25.
//
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    char c[]="abcdef";
    //从c[0]开始，获取n个字符，构造str1;
    string str1(c,0,strlen(c));
    std::cout<<str1<<endl;

    //在末尾追加字符串c
    str1.append(c);
    std::cout<<str1<<endl;
    //等价于
    str1=str1+c;
    std::cout<<str1<<endl;

    //将string转换为const char*
    const char* c1 = str1.data();
    std::cout<<c1<<endl;
    const char* c2 = str1.c_str();
    std::cout<<c2<<endl;

    //从str1从5拷贝str1.size()个字符到buffer；
    //从字符串(string)拷贝字符到字符数组(char[])
    char buffer[20]={0};
    str1.copy(buffer,str1.size(),5);
    std::cout<<buffer<<endl;

    //查找字符串或字符,查找失败返回-1或string::npos(无符号整数)
    //find/find_not_of/find_last_of/find_last_not_of
    //从4开始查找'a'字符
    if (str1.find('a',4)!=string::npos)
    {
        cout<<str1.find('a',4)<<endl;
    }
    if (str1.find("def",4)!=string::npos)
    {
        cout<<str1.find("def",4)<<endl;
    }

    //从str1[5]开始，删除2个
    str1.erase(5,2);
    cout<<str1<<endl;

    //从str1[5]开始插入abc
    str1.insert(5,"abc");
    cout<<str1<<endl;

    //截取字符串
    //从str1[0]开始截取5个字符
    string str2=str1.substr(0,5);
    cout<<str2<<endl;
    //从str1[3]开始截取到末尾
    string str3 = str1.substr(3);
    cout<<str3<<endl;
    string str4 = str1.substr(3,string::npos);
    cout<<str4<<endl;

    return 0;
}